package ru.amse.tsyganov.jumleditor.properties;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import ru.amse.tsyganov.jumleditor.commands.ViewsManager;
import ru.amse.tsyganov.jumleditor.graphicaleditor.GraphicalEditor;
import ru.amse.tsyganov.jumleditor.view.common.ViewElement;

public class PropertiesTable {
	
	public static final String PROPERTY_NAME = "Property";
	
	public static final String PROPERTY_VALUE = "Value";
	
	public static final int PROPERTY_NAME_COLUMN_INDEX = 0;
	
	public static final int PROPERTY_VALUE_COLUMN_INDEX = 1;
	
	private ViewElement source;
	
	private final Table propertiesTable;
	
	private final TableViewer propertiesTableViewer;
	
	private final PropertiesContentProvider propertiesContentProvider;
	
	private final PropertiesCellModifier propertiesCellModifier;
	
	private final PropertyChangeListener propertyChangeListener = 
		new PropertyChangeListener() {

		public void propertyChanged(Property p) {
			propertiesTableViewer.update(p, null);
		}
		
	};

	public PropertiesTable(Composite parent, int tableWidth,
			ViewsManager viewsManager, GraphicalEditor graphicalEditor) {
		
		propertiesTable = new Table(parent, 
				SWT.SINGLE | SWT.FULL_SELECTION | 
				SWT.V_SCROLL | SWT.H_SCROLL);
	
		initTable(tableWidth);
		
		propertiesTableViewer = new TableViewer(propertiesTable);
		
		propertiesContentProvider = new PropertiesContentProvider(
				new PropertiesList());
		
		propertiesCellModifier = new PropertiesCellModifier(
				propertiesTableViewer, viewsManager, graphicalEditor);
		
		initTableViewer();
	}

	private void initTableViewer() {
		propertiesTableViewer.setUseHashlookup(true);
		
		propertiesTableViewer.setLabelProvider(new PropertiesLabelProvider());
		
		propertiesTableViewer.setContentProvider(propertiesContentProvider);
		
		propertiesTableViewer.setCellEditors(new CellEditor[] {
			new TextCellEditor(propertiesTable),
			new TextCellEditor(propertiesTable),
		});
		
		propertiesTableViewer.setColumnProperties(
				new String[] {PROPERTY_NAME, PROPERTY_VALUE});
				
		propertiesTableViewer.setCellModifier(propertiesCellModifier);
		
		propertiesTableViewer.setInput(new PropertiesList());
	}

	private void initTable(int tableWidth) {
		TableColumn propertyNameColumn = 
			new TableColumn(propertiesTable, SWT.LEFT);
		propertyNameColumn.setText(PROPERTY_NAME);
		propertyNameColumn.setWidth(tableWidth / 2);
		
		TableColumn propertyValueColumn = 
			new TableColumn(propertiesTable, SWT.LEFT);
		propertyValueColumn.setText(PROPERTY_VALUE);
		propertyValueColumn.setWidth(tableWidth / 2);
				
		propertiesTable.setHeaderVisible(true);
		propertiesTable.setLinesVisible(true);
	}

	public Table getTable() {
		return propertiesTable;
	}

	public void setSource(ViewElement viewElement) {
		if (source != null) {
			source.removePropertyListener(propertyChangeListener);
		}
		
		this.source = viewElement;
		
		viewElement.addPropertyListener(propertyChangeListener);
		
		propertiesTableViewer.setInput(viewElement.getProperties());
	}
	
	public void clear() {
		propertiesTableViewer.setInput(new PropertiesList());
	}
}
